package quicksorttester;

/**
 *
 * @author 
 */
import java.util.Arrays;
import java.util.Random;


public class Tests {
    private Integer[] numbers;
    private int maxInt;

    public Tests() {
        this.maxInt = 40;
    }
    
    public Tests(int maxInt) {
        this.maxInt = maxInt;
    }
    
    public void performTest(int mode, QuickSort q, int maxStep){
        switch(mode)
        {
            case 0://random
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateNumbers(i, maxInt);
                    //System.out.println(arrayToString());
                    int result = q.enableQuickSort(numbers);
                    if(result != -1){
                        System.out.println("For random array nr"+1+" of size: "+i+" the number of comparisons is: "+result);   
                    }
                    //System.out.println(arrayToString());
                    
                    generateNumbers(i, maxInt);
                    result = q.enableQuickSort(numbers);
                    if(result != -1){
                        System.out.println("For random array nr"+2+" of size: "+i+" the number of comparisons is: "+result);
                    }
                    
                    generateNumbers(i, maxInt);
                    result = q.enableQuickSort(numbers);
                    if(result != -1){
                        System.out.println("For random array nr"+3+" of size: "+i+" the number of comparisons is: "+result);
                    }
                }
            }
                break;
            case 1://optimistic
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateAscArray(i);System.out.println(arrayToString());
                    int result = q.enableQuickSort(numbers);
                    if(result != -1){
                        System.out.println("For sorted array of size: "+i+" the number of comparisons is: "+result);
                    }System.out.println(arrayToString());
                }
            }
                break;
            case 2://pesimistic
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateDescArray(i);System.out.println(arrayToString());
                    int result = q.enableQuickSort(numbers);
                    if(result != -1){
                        System.out.println("For reversed array of size: "+i+" the number of comparisons is: "+result);
                    }System.out.println(arrayToString());
                }
            }
                break;
            case 3://all
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateNumbers(i, maxInt);
                    int result = q.enableQuickSort(numbers);
                    if(result != -1){
                        //System.out.println("For random array nr"+1+" of size: "+i+" the number of comparisons is: "+result);
                        System.out.print(i+";"+result+";");
                    }
                    
                    generateNumbers(i, maxInt);
                    result = q.enableQuickSort(numbers);
                    if(result != -1){
                        //System.out.println("For random array nr"+2+" of size: "+i+" the number of comparisons is: "+result);
                        System.out.print(result+";");
                    }
                    
                    generateNumbers(i, maxInt);
                    result = q.enableQuickSort(numbers);
                    if(result != -1){
                        //System.out.println("For random array nr"+3+" of size: "+i+" the number of comparisons is: "+result);
                        System.out.print(result+";");
                    }
                    
                    generateAscArray(i);
                    result = q.enableQuickSort(numbers);
                    if(result != -1){
                        //System.out.println("For sorted array of size: "+i+" the number of comparisons is: "+result);
                        System.out.print(result+";");
                    }
                    
                    generateDescArray(i);
                    result = q.enableQuickSort(numbers);
                    if(result != -1){
                        //System.out.println("For reversed array of size: "+i+" the number of comparisons is: "+result);
                        System.out.print(result+";\n");
                    }
                }
            }
                break;
        }
    }
    
    public void performTimeTests(int mode, QuickSort q, int maxStep){
        int timeMode = 1;
        long startTime;
        switch(mode)
        {
            case 0://random
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateNumbers(i, maxInt);
                    startTime = initClock();
                    int result = q.enableQuickSort(numbers);
                    long estTime = stopClock(1,startTime);
                    if(result != -1){
                        System.out.println("For random array nr"+1+" of size: "+i+" the quicksort works: "+estTime);   
                    }
                    
                    generateNumbers(i, maxInt);
                    startTime = initClock();
                    result = q.enableQuickSort(numbers);
                    estTime = stopClock(1,startTime);
                    if(result != -1){
                        System.out.println("For random array nr"+2+" of size: "+i+" the quicksort works: "+estTime);   
                    }
                    
                    generateNumbers(i, maxInt);
                    startTime = initClock();
                    result = q.enableQuickSort(numbers);
                    estTime = stopClock(1,startTime);
                    if(result != -1){
                        System.out.println("For random array nr"+3+" of size: "+i+" the quicksort works: "+estTime);   
                    }
                }
            }
                break;
            case 1://optimistic
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateAscArray(i);
                    startTime = initClock();
                    int result = q.enableQuickSort(numbers);
                    long estTime = stopClock(1,startTime);
                    if(result != -1){
                        System.out.println("For sorted array of size: "+i+" the quicksort works: "+estTime);   
                    }
                }
            }
                break;
            case 2://pesimistic
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateDescArray(i);
                    startTime = initClock();
                    int result = q.enableQuickSort(numbers);
                    long estTime = stopClock(1,startTime);
                    if(result != -1){
                        System.out.println("For reversed array of size: "+i+" the quicksort works: "+estTime);   
                    }
                }
            }
                break;
            case 3://all
            {
                for(int i = 10; i <= maxStep; i+=10)
                {
                    generateNumbers(i, maxInt);
                    startTime = initClock();
                    int result = q.enableQuickSort(numbers);
                    long estTime = stopClock(1,startTime);
                    if(result != -1){
                        //System.out.println("For random array nr"+1+" of size: "+i+" the quicksort works: "+estTime); 
                        System.out.print(i+";"+estTime+";");
                    }
                    
                    generateNumbers(i, maxInt);
                    startTime = initClock();
                    result = q.enableQuickSort(numbers);
                    estTime = stopClock(1,startTime);
                    if(result != -1){
                        //System.out.println("For random array nr"+2+" of size: "+i+" the quicksort works: "+estTime);
                        System.out.print(estTime+";");
                    }
                    
                    generateNumbers(i, maxInt);
                    startTime = initClock();
                    result = q.enableQuickSort(numbers);
                    estTime = stopClock(1,startTime);
                    if(result != -1){
                        //System.out.println("For random array nr"+3+" of size: "+i+" the quicksort works: "+estTime); 
                        System.out.print(estTime+";");
                    }
                    
                    generateAscArray(i);
                    startTime = initClock();
                    result = q.enableQuickSort(numbers);
                    estTime = stopClock(1,startTime);
                    if(result != -1){
                        //System.out.println("For sorted array of size: "+i+" the quicksort works: "+estTime);
                        System.out.print(estTime+";");
                    }
                    
                    generateDescArray(i);
                    startTime = initClock();
                    result = q.enableQuickSort(numbers);
                    estTime = stopClock(1,startTime);
                    if(result != -1){
                        //System.out.println("For reversed array of size: "+i+" the quicksort works: "+estTime);
                        System.out.print(estTime+";\n");
                    }
                }
            }
                break;
        }
    }
        
    public void generateNumbers(int s, int max){
        setNumbers(new Integer[s]);
    Random generator = new Random();
      for (int i = 0; i < getNumbers().length; i++) {
            getNumbers()[i] = generator.nextInt(max);
  }
    }
    
    public void generateAscArray(int s){
        setNumbers(new Integer[s]);
            for (int i = 0; i < getNumbers().length; i++) {
            getNumbers()[i] = i;
  }
    }
    
    public void generateDescArray(int s){
        setNumbers(new Integer[s]);
            for (int i = 0; i < getNumbers().length; i++) {
            getNumbers()[i] = s-i;
  }
    }
    /**
    * @return the numbers
    */
    public Integer[] getNumbers() {
        return numbers;
    }
    
    /**
    * @param numbers the numbers to set
    */
    public void setNumbers(Integer[] numbers) {
        this.numbers = numbers;
    }
   
    /**
     * @return converted array of numbers into string 
     */
    public String arrayToString(){
        return Arrays.toString(numbers);
    }
    
    public long initClock() {
        return System.nanoTime();
    }
    
    public long stopClock(int mode, long startTime){
        
        long estimatedTime = System.nanoTime() - startTime;
        
        switch(mode){
            case 0:           
                estimatedTime = estimatedTime/1000000; //miliseconds
                break;
            case 1:
                estimatedTime = estimatedTime/1000; //microseconds
                break;
            //case 2:
            //  estimatedTime = estimatedTime; :)) //nanoseconds
            //  break;
        }
        
        return estimatedTime;
    }
    
    public void reverseOrder(int[] number){
        int tmp;
        for(int k=0; k<number.length/2; k++){
            tmp = number[k];
            number[k] = number[number.length-k-1];
            number[number.length-k-1] = tmp;
        }
        
    }
   
//    /**
//     * Test with randomly generated array with size and maximum value previously 
//     * defined in constructor or size = 50 and maxInt = 40.
//     * 
//     * @param qs : QuickSort object
//     * @param mode : determines in which unit the result will be displayed.
//     * The possible modes are as follows:
//     * 0: miliseconds
//     * 1: microseconds
//     * 2: nanoseconds
//     * 
//     */
//    public void performTest(QuickSort qs, int mode)
//    {
//        long startTime = System.nanoTime(); 
//        //qs.performSorting();
//        long estimatedTime = System.nanoTime() - startTime;
//        System.out.println("");
//        switch(mode)
//        {
//            case 0://miliseconds
//            {
//                System.out.println("Quicksort took " + estimatedTime/1000000 + " miliseconds");
//            }
//                break;
//            case 1://microseconds
//            {
//                System.out.println("Quicksort took " + estimatedTime/1000 + " microseconds");
//            }
//                break;
//            case 2://nanoseconds
//            {
//                System.out.println("Quicksort took " + estimatedTime + " nanoseconds");
//            }
//        }        
//    }
//    
//    /**
//     * Test with specified array.
//     * 
//     * @param qs : QuickSort object
//     * @param array : 
//     * @param mode : determines in which unit the result will be displayed.
//     * The possible modes are as follows:
//     * 0: miliseconds
//     * 1: microseconds
//     * 2: nanoseconds
//     * 
//     */
//    public void performTest(QuickSort qs, int[] array, int mode)
//    {
//        setNumbers(array);
//        performTest(qs,mode);
//    }


    
    
}
